/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-08-16
 * Time: 19:39
 */
//https://leetcode.cn/problems/fruit-into-baskets/
public class Test {
    public static int totalFruit(int[] arr) {
        int size=arr.length;
        int[] hash=new int[size];
        int z=0,max=0;
        for(int p1=0,p2=0;p2<size;p2++){
            if(hash[arr[p2]]==0){
                while(z==2){
                    hash[arr[p1]]--;
                    if(hash[arr[p1]]==0){
                        z--;
                    }
                    p1++;
                }
                hash[arr[p2]]++;
                z++;
            }
            else{
                hash[arr[p2]]++;
            }
            max=Math.max(max,p2-p1+1);
        }
        return max;
    }
    public static void main(String[] args) {
        int[] arr={1,2,3,2,2};
        System.out.println(totalFruit(arr));
    }
}
